<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
/**
 * 基础类控制器控制器
 * Class Adminuser
 * @package app\index\controller
 */
class Base extends Controller
{
    /**
     * page当前页码
     * @var string
     */
    public $page = '';
    /**
     * 每页显示多少条
     * @var string
     */
    public $size = '';
    /**
     * 查询条件的起始值
     * @var int
     */
    public $from = 0;
    /**
     * 管理员信息
     * @var int
     */
    public $user = array();

    public function _initialize()
    {
        $param = input('param.');

        $hasUser = Db::name('admin_user')->where('token', $param['token'])->find();
        if(empty($param['token']) || empty($hasUser)){
            json(array("code"=>10002,"msg"=>"Sorry! See you later!"))->send();die;
        }
        //判断会话是否过期
        if (time() - $hasUser['update_time'] > config('admin.token_expire')) {
            json(array("code"=>10003,"msg"=>"Token expired!"))->send();die;
        }

        $auth = new \com\Auth();
        $module     = strtolower(request()->module());
        $controller = strtolower(request()->controller());
        $action     = strtolower(request()->action());
        $url        = $module."/".$controller."/".$action;

        if($hasUser['groupid']!=1 && $hasUser['is_zhu'] == 1){
            if(!in_array($url, config('admin.permission_authlist')))
            {
                if(!$auth->check($url,$hasUser['id']))
                {
                    json(array("code"=>10004,"msg"=>"Sorry, you do not have permission to operate!"))->send();die;
                }
            }
        }
        $this->user = $hasUser;

    }

    /**
     * [isLogin  判定是否登录]
     * @author [C。]
     * @return bool
     */
    public function isLogin() {
        //获取session
        $adminuser = cache(config('admin.session_adminuser'), '', config('admin.session_adminuser_scope'));
        if($adminuser && $adminuser['id']) {
            return true;
        }
        return false;
    }


    /**
     * [getPageAndSize  获取分页page size 内容]
     * @author [C。]
     * @param $data
     */
    public function getPageAndSize($data) {
        $this->page = !empty($data['page']) ? $data['page'] : 1;
        $this->size = !empty(config('list_rows')) ? config('list_rows') : config('paginate.list_rows');//后台设置显示条数、配置文件显示条数
        $this->from = ($this->page - 1) * $this->size;
    }
}